﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace ViewMaker.Core.Silverlight
{
    public class TreeViewItemBehavior : Behavior<TreeView>
    {
        public TreeViewItemBehavior()
        {

        }

        public static readonly DependencyProperty SelectedObjectProperty = DependencyProperty.Register("SelectedObject", typeof(object), typeof(TreeViewItemBehavior), new PropertyMetadata(SelectedObjectCallback));

        public object SelectedObject
        {
            get { return this.GetValue(TreeViewItemBehavior.SelectedObjectProperty); }
            set { this.SetValue(TreeViewItemBehavior.SelectedObjectProperty, value); }
        }


        private static void SelectedObjectCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.SelectedItemChanged += AssociatedObject_SelectedItemChanged;
        }

        void AssociatedObject_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            SelectedObject = AssociatedObject.SelectedItem;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.SelectedItemChanged -= AssociatedObject_SelectedItemChanged;
        }

    }

}
